﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlServerCe;

namespace WinSat
{
    public enum ProgramTipleri
    {
        DirectSql,
        XmlServis,
        SqlCompact
    }

    public class Query : IDisposable
    {
        ProgramTipleri programTipi = ProgramTipleri.DirectSql;
        List<SqlParameter> _prm2 = new List<SqlParameter>();
        List<SqlCeParameter> _prm3 = new List<SqlCeParameter>();

        public Query()
        {
            if (Program.Session[Settings.BaglantiTipi].ToString() == "servis")
            {
                programTipi = ProgramTipleri.XmlServis;
                Program.servis.Url = Program.Session[Settings.Sunucu].ToString();
                Program.servis.Timeout = 60000;
            }
            else if (Program.Session[Settings.BaglantiTipi].ToString() == "local")
            {
                programTipi = ProgramTipleri.SqlCompact;
            }
        }

        public Query(ProgramTipleri programtipi)
        {
            programTipi = programtipi;
        }

        private string _queryString = string.Empty;
        public Query Add(string sqlText)
        {
            this._queryString = sqlText;
            return this;
        }
        public void Clear()
        {
            this._queryString = string.Empty;
            if (programTipi == ProgramTipleri.DirectSql)
            {
                this._prm2.Clear();
            }
            else if (programTipi == ProgramTipleri.SqlCompact)
            {
                _prm3.Clear();
            }
            else
            {
                Program.servis.Clear(Program.SessionID);
            }
        }
        public Query Add(string name, SqlDbType type, object value)
        {
            if (value is string)
            {
                value = value.ToString().Replace('*', '%');
            }
            if (programTipi == ProgramTipleri.DirectSql)
            {
                _prm2.Add(new SqlParameter() { ParameterName = name, SqlDbType = type, Value = value });
            }
            else if (programTipi == ProgramTipleri.SqlCompact)
            {
                _prm3.Add(new SqlCeParameter() { ParameterName = name, SqlDbType = type, Value = value });
            }
            else
            {
                Program.servis.AddParameter1(name, (byte)type, value, Program.SessionID);
            }
            return this;
        }
        public Query Add(string name, SqlDbType type, object value, int size)
        {
            if (value is string)
            {
                value = value.ToString().Replace('*', '%');
            }
            if (programTipi == ProgramTipleri.DirectSql)
            {
                _prm2.Add(new SqlParameter() { ParameterName = name, SqlDbType = type, Value = value, Size = size });
            }
            else if (programTipi == ProgramTipleri.SqlCompact)
            {
                _prm3.Add(new SqlCeParameter() { ParameterName = name, SqlDbType = type, Value = value, Size = size });
            }
            else
            {
                Program.servis.AddParameter2(name, (byte)type, value, size, Program.SessionID);
            }
            return this;
        }
        public void SetParamValue(int idx, object value)
        {
            if (programTipi == ProgramTipleri.DirectSql)
            {
                _prm2[idx].Value = value;
            }
            else if (programTipi == ProgramTipleri.SqlCompact)
            {
                _prm3[idx].Value = value;
            }
            else
            {
                Program.servis.ParameterValue(idx, value, Program.SessionID);
            }
        }
        public List<T> ToList<T>(T sample)
        {
            return SqlServer.Get.ToList<T>(sample, this._queryString.Replace('*', '%'), this._prm2.ToArray());
        }
        public void Exec()
        {
            if (programTipi == ProgramTipleri.DirectSql)
            {
                SqlServer.Get.Exec(_queryString.Replace('*', '%'), null, _prm2.ToArray());
            }
            else if (programTipi == ProgramTipleri.SqlCompact)
            {
                DbSqlCe.Get.Exec(_queryString.Replace('*', '%'), null, _prm3.ToArray());
            }
            else
            {
                Program.servis.Exec(_queryString.Replace('*', '%'), Program.SessionID);
            }
        }
        public T ExecScaler<T>()
        {
            if (programTipi == ProgramTipleri.DirectSql)
            {
                if (_prm2.Count > 0)
                {
                    return SqlServer.Get.ExecScalar<T>(_queryString.Replace('*', '%'), _prm2.ToArray());
                }
                else
                {
                    return SqlServer.Get.ExecScalar<T>(_queryString.Replace('*', '%'));
                }
            }
            else if (programTipi == ProgramTipleri.SqlCompact)
            {
                if (_prm3.Count > 0)
                {
                    return DbSqlCe.Get.ExecScalar<T>(_queryString.Replace('*', '%'), _prm3.ToArray());
                }
                else
                {
                    return DbSqlCe.Get.ExecScalar<T>(_queryString.Replace('*', '%'));
                }
            }
            else
            {
                object sourceValue = Program.servis.ExecScalar(_queryString.Replace('*', '%'), Program.SessionID);
                if (sourceValue == null || sourceValue == DBNull.Value)
                    return (T)Cevir.DefaultSetting(typeof(T).ToString());
                try
                {
                    return (T)Convert.ChangeType(sourceValue, typeof(T), null);
                }
                catch
                {
                    return (T)Cevir.DefaultSetting(typeof(T).ToString());
                }
            }
        }
        public DataTable Fill()
        {
            if (programTipi == ProgramTipleri.DirectSql)
            {
                return SqlServer.Get.Fill(_queryString.Replace('*', '%'), _prm2.ToArray());
            }
            else if (programTipi == ProgramTipleri.SqlCompact)
            {
                return DbSqlCe.Get.Fill(_queryString.Replace('*', '%'), _prm3.ToArray());
            }
            else
            {
                return Program.servis.Fill(_queryString.Replace('*', '%'), Program.SessionID).Tables[0];
            }
        }
        //public void OpenTran()
        //{
        //    if (programTipi == ProgramTipleri.DirectSql)
        //    {
        //        trn = SqlServer.Get.OpenTransaction();
        //    }
        //    else
        //    {
        //        Program.servis.OpenTran(Program.SessionID);
        //    }
        //}
        //public bool Commit()
        //{
        //    bool sonuc = false;
        //    if (programTipi == ProgramTipleri.DirectSql)
        //    {
        //        if (trn != null)
        //        {
        //            try
        //            {
        //                trn.Commit();
        //                sonuc = true;
        //            }
        //            catch (Exception)
        //            {
        //                if (trn != null)
        //                {
        //                    trn.Rollback();
        //                    trn = null;
        //                }
        //            }
        //        }
        //    }
        //    else
        //    {
        //        sonuc = Program.servis.CommitTran(Program.SessionID);
        //    }
        //    return sonuc;
        //}
        public void Dispose()
        {
            if (programTipi == ProgramTipleri.XmlServis)
            {
                Program.servis.Clear(Program.SessionID);
            }
            GC.SuppressFinalize(_prm2);
            GC.SuppressFinalize(_prm3);
            GC.SuppressFinalize(this);
        }
    }
}
